package com.service.impl;

import com.bean.Notice;
import com.dao.NoticeDao;
import com.dao.impl.NoticeDaoImpl;
import com.service.NoticeService;
import com.utils.Page;

import java.util.List;

public class NoticeServiceImpl implements NoticeService {
    NoticeDao noticeDao = new NoticeDaoImpl();

    @Override
    public Page<Notice> page(int pageNo, int pageSize, String content) {
        Page<Notice> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = noticeDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Notice> items = noticeDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Notice selectBy(Notice notice) {
        return noticeDao.selectBy(notice);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        if (noticeDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int updeteInfo(Notice record) {
        int status = 0;
        if (noticeDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(Notice record) {
        int status = 0;
        if (noticeDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }
}
